package com.saifan.wyy_ov.ui.property;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.MatterRequsetBean;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.CameraUtil;
import com.saifan.wyy_ov.utils.DialogUtil;
import com.saifan.wyy_ov.utils.FileUtils;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.LogUtil;
import com.saifan.wyy_ov.utils.PictureUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.Date;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-29 17:34:13
 * @descrption 随手拍
 */

public class PhotographActivity extends BaseActivity implements TextWatcher {
    private static final int SELECT_PHOTO_TAKEPHOTO = 1;
    private Toolbar toolbar;

    String picName;
    String  fileName;
    private ImageView imageView;
    private EditText content;
    MatterRequsetBean requsetBean;
    private RoomInfoBean roomInfoBean;
    private TextView contentnumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        picName = getIntent().getStringExtra("picName");
        roomInfoBean = getRoomInfoBean();

        setContentView();

    }

    public void setContentView() {
        setContentView(R.layout.activity_photograph);
        this.contentnumber = (TextView) findViewById(R.id.content_number);
        this.content = (EditText) findViewById(R.id.content);
        this.imageView = (ImageView) findViewById(R.id.imageView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (picName != null) {
            ImgUtil.DisPlayImgForLocal(picName, imageView, PhotographActivity.this);
        }
        content.addTextChangedListener(this);
    }

    public String initParams() {
        requsetBean = new MatterRequsetBean();
        requsetBean.setBSQD_BSLR(content.getText().toString().trim());

        requsetBean.setBSQD_CJSJ(new Date());
        requsetBean.setBSQD_LXR(roomInfoBean.getWYKHDA_KHMC());
        requsetBean.setBSQD_LXDH(roomInfoBean.getWYKHDA_SJ());
        requsetBean.setBSQD_LX("随手拍");
        requsetBean.setBSQD_XMLX(1);
        requsetBean.setBSQD_FKGXWJ(roomInfoBean.getFKGX_ZJ());
        requsetBean.setBSQD_DZ(roomInfoBean.getWYFJDA_FJMC());
        requsetBean.setXMBS(roomInfoBean.getXMBS());
        return new Gson().toJson(requsetBean);

    }




    public void submit(View view) {
        if (isEmpty(content)) {
            content.setFocusable(true);
            ToastUtil.showToast(this, "内容不能为空");
            return;
        }
        GetData getData = new GetData();
        RequestParams params = new RequestParams();
        params.addBodyParameter(Api.Matter.MATTER, initParams());
        File file = new File(PictureUtil.getCompressBitmap(picName));
        if (file.exists()) {
            LogUtil.i(FileUtils.getFileSize(FileUtils.getFileSize(file.getPath())));
            params.addBodyParameter(file.getName(), file);
        }
        getData.getData(Api.Matter.MATTER_SUBMIT, params, new RequestCallBack<String>() {

            @Override
            public void onStart() {
                super.onStart();
                DialogUtil.showProgressDialog(PhotographActivity.this, "正在上传图片");
            }

            @Override
            public void onCancelled() {
                super.onCancelled();
                LogUtil.i("取消请求");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                if (!StringUtil.isNullOrEmpty(responseInfo.result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(responseInfo.result.toString());
                        String code = jsonObject.getString("Code");
                        if (code.equals("200")) {
                            ToastUtil.showToast(PhotographActivity.this, "提交成功");
                            DialogUtil.closeProgressDialog();
                            finish();

                        } else if (code.equals("203")) {
                            DialogUtil.closeProgressDialog();
                            ToastUtil.showToast(PhotographActivity.this, getString(R.string.login_error_tip));
                        } else if (code.equals("202")) {
                            DialogUtil.closeProgressDialog();
                            ToastUtil.showToast(PhotographActivity.this, "保存失败，信息未做修改 !");
                        } else {
                            LogUtil.e(code);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    LogUtil.e("ServerError");
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                LogUtil.i(s);
                ToastUtil.showToast(PhotographActivity.this, getString(R.string.network_error));
                DialogUtil.closeProgressDialog();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
                LogUtil.i("onLoading" + current + "-------------------" + total);
            }
        });


    }


    public void cancel(View view) {
        finish();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        contentnumber.setText(s.length() + "/" + 100);

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public void opencamer(View view) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, SELECT_PHOTO_TAKEPHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_PHOTO_TAKEPHOTO) {
            if (resultCode == RESULT_OK) {
                Bitmap bitmap = data.getParcelableExtra("data") ;
                File file = FileUtils. bitmapToFile(bitmap);
                FileUtils.toAlbum(this,file);
                picName=file.getAbsolutePath();
                if (picName != null) {
                    ImgUtil.DisPlayImgForLocal(picName, imageView, PhotographActivity.this);
                }
            }
        }
    }
}
